home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / kcontainer.h.z / kcontainer.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  9.0 KB  |  263 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1998 Jorge Monteiro <jomo@casema.net>
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.     Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #ifndef __KCONTAINER_H__
  21. #define __KCONTAINER_H__
  22.  
  23. #include <qframe.h>
  24. #include <qlist.h>
  25.  
  26. /**
  27.  * KContainerLayoutItem is a internal class used by KConatinerLayout 
  28.  * @see KContainerLayout
  29.  * This class represents one widget inside the one KContainerLayout
  30.  * 
  31.  * @short KContainerLayoutItem
  32.  * @internal
  33.  * @author Jorge Monteiro <jomo@casema.net>
  34.  * @version 0.1
  35.  */
  36. class KContainerLayoutItem
  37. {
  38. public:
  39.     /**
  40.      * Creates one KContIem
  41.      *
  42.      * @param w The widget associated with this KContainerLayoutItem
  43.      * @param e Expand will make the widget to use all space alocated to it
  44.      * @param f Fill will make the widget be sized to ocupy all the space allocated to it. Only makes sense with expand = TRUE
  45.      * @param p Padding is the size the widget will use as borders on both sides of the space allocated to it.
  46.      */
  47.     KContainerLayoutItem(QWidget *w,bool e=FALSE,bool f=FALSE,int p=0)
  48.     {
  49.     _widget = w;
  50.     _expand = e;
  51.     _fill = f;
  52.     _padding = p;
  53.     }
  54.     void setExpand(bool b)        { _expand = b; }
  55.     void setFill(bool b)        { _fill = b; }
  56.     void setPadding(int i)        { _padding = i; }
  57.     QWidget *widget()            { return _widget; }
  58.     const bool expand() const        { return _expand; }
  59.     const bool fill() const        { return _fill; }
  60.     const int padding() const        { return _padding; }
  61. protected:
  62.     QWidget *_widget;
  63.     bool _expand;
  64.     bool _fill;
  65.     int _padding;
  66. };
  67.  
  68. /**
  69.  * KContainerLayout is a class that will manage layout of it's child widgets.
  70.  * Here is an example of using this class:
  71.  *
  72.  * 
  73.  * @short KContainerLayout is a class that will manage layout of it's child widgets.
  74.  * @author Jorge Monteiro <jomo@casema.net>
  75.  * @version 0.1
  76.  */
  77. class KContainerLayout : public QFrame
  78. {
  79. Q_OBJECT
  80. public:
  81. enum { Horizontal = 0, Vertical };
  82.     /**
  83.      * Constructs a KContainerLayout as child of parent
  84.      * 
  85.      * @param parent The parent widget of the KContainerLayout
  86.      * @param name The name of the widget
  87.      * @param orientation The orientation of the container, either KContainerLayout::Horizontal or KContainer::Vertical
  88.      * @param homogeneous If the container should split available size by all KContainerLayoutItem in equal parts
  89.      * @param spacing The space to add between each widget and between the first/last and the borders
  90.      * @param f Flags @see QFrame#QFrame
  91.      * @param allowLines Flags @see QFrame#QFrame
  92.      */
  93.     KContainerLayout(QWidget * parent=0, const char * name=0, 
  94.             int orientation = KContainerLayout::Horizontal,
  95.         bool homogeneos = FALSE,
  96.         int spacing = 5, WFlags f=0, bool allowLines=TRUE);
  97.     /**
  98.      * The destructor
  99.      */
  100.     virtual ~KContainerLayout();
  101.     /**
  102.      * Returns the number of widgets inside this container
  103.      */
  104.     int getNumberOfWidgets() const;
  105.     /**
  106.      * Pack one widget to the start of the container after the previously packed on start widgets
  107.      * @param w The widget to be packed
  108.      * @param e If the widget should use the whole size allocated to it
  109.      * @param f If the widget should be sized to fill the whole space allocated to it (only makes sense with Expand=TRUE)
  110.      * @param p Padding that should be used as the borders in each side of the widget
  111.      */
  112.     int packStart(QWidget *w, bool e=FALSE, bool f=FALSE,int p=1);
  113.     /**
  114.      * Pack one widget to the end of the container after the previously packed on end widgets
  115.      * @param w The widget to be packed
  116.      * @param e If the widget should use the whole size allocated to it
  117.      * @param f If the widget should be sized to fill the whole space allocated to it (only makes sense with Expand=TRUE)
  118.      * @param p Padding that should be used as the borders in each side of the widget
  119.      */
  120.     int packEnd(QWidget *w, bool e=FALSE, bool f=FALSE,int p=1);
  121.     /**
  122.      * Sets the orientation of the container, one of KContainerLayout::Horizontal or KContainer::Vertical
  123.      */
  124.     void setOrientation(int i);
  125.     /**
  126.      * Sets the container to use the same size to each widget he contains (TRUE) or not (FALSE)
  127.      * When homogeneous is true all widgets will be packed as if they had the Expand set to TRUE @see KContainerLayout#packStart @see KContainer#packEnd
  128.      */
  129.     void setHomogeneos(bool b);
  130.     /*
  131.      * Sets the space to be used between each widget and between the first/last widget and the container borders
  132.      */
  133.     void setSpacing(int i);
  134.     /**
  135.      * Sets the starting offset for this container @see _startOffset
  136.      */
  137.     void setStartOffset(int i);
  138.     /**
  139.      * Sets the ending offset for this container @see _endOffset
  140.      */
  141.     void setEndOffset(int i);
  142.     /**
  143.      * Returns the current orientation of the container @see KContainerLayout#setOrientation
  144.      */
  145.     const int orientation() const    { return _orientation; }
  146.     /**
  147.      * Returns the current homogeneous state of the container @see KContainerLayout#setHomogeneous
  148.      */
  149.     const bool homogeneos() const    { return _homogeneos; }
  150.     /**
  151.      * Returns the current spacing of the container @see KContainerLayout#setSpacing
  152.      */
  153.     const int spacing() const        { return _spacing; }
  154.     /**
  155.      * Returns the starting offset for this container @see _startOffset
  156.      */
  157.     const int startOffset() const    { return _startOffset; }
  158.     /**
  159.      * Returns the ending offset for this container @see _endOffset
  160.      */
  161.     const int endOffset() const        { return _endOffset; }
  162.     /**
  163.      * Resizes the container to be as small as necessary to display all widgets
  164.      */
  165.     void sizeToFit();
  166. protected:
  167.     /**
  168.      * Calculates the size necessary to display all widgets
  169.      */
  170.     void recalcLayout();
  171.     /**
  172.      * Returns the size necessary for the widget represented by this KContainerLayoutItem
  173.      */
  174.     QSize widgetSize(KContainerLayoutItem *item)
  175.     {
  176.     QSize sz = item->widget()->sizeHint();
  177.     if (!sz.isValid())
  178.        // well, some widgets will not return sizeHint()
  179.         sz = QSize(50,25); 
  180.     return sz;
  181.     }
  182.     /**
  183.      * Reposition all widgets on the container.
  184.      */
  185.     void repositionWidgets();
  186.     /**
  187.      * Returns the number of widgets that share the extra space on the container.
  188.      */
  189.     int numberOfWidgetsWithExpand();
  190.     /**
  191.      * Calculate our size hint based on the sizeHint() of all out widgets,
  192.      * on our properties - expand and homogeneous, and on the KContainerLayoutItem 
  193.      * properties.
  194.      * @see KContainerLayoutItem @see packStart @see packEnd
  195.      */
  196.     void calculateSizeHint();
  197.     /**
  198.      * Return the size necessary by the largest widget on the container.
  199.      */
  200.     QSize sizeOfLargerWidget();
  201.     /**
  202.      * Returns the ideal size for the widget represented by this KContainerLayoutItem.
  203.      */
  204.     QSize idealSizeOfWidget(KContainerLayoutItem *item);
  205.     /**
  206.      * Return TRUE if this is an horizontal container.
  207.      */
  208.     const bool horizontal() const     
  209.     { return (_orientation == KContainerLayout::Horizontal); }
  210.     /**
  211.      * Resizes the widget and repositions all widgets.
  212.      */
  213.     virtual void resizeEvent(QResizeEvent *ev);
  214.     /**
  215.      * Used to filter resize events from our parent if it isn't a KContainerLayout.
  216.      */
  217.     virtual bool eventFilter(QObject *, QEvent *ev);
  218.     /**
  219.      * Returns our size hint. The size necessary to display this container.
  220.      */
  221.     virtual QSize sizeHint() const;
  222.     /**
  223.      * Keeps the orientation of the container one of
  224.      * KContainerLayout::Horizontal or KContainer::Vertical
  225.      */
  226.     int _orientation;
  227.     /**
  228.      * Should we split our size in equal parts by all the widgets?
  229.      */ 
  230.     bool _homogeneos;
  231.     /**
  232.      * Space to be used between widgets and between the first/last widget and
  233.      * the container borders.
  234.      */
  235.     int _spacing;
  236.     /**
  237.      * Space between the starting border and the first widget
  238.      */
  239.     int _startOffset;
  240.     /**
  241.      * Space between the last widget and ending border
  242.      */
  243.     int _endOffset;
  244.     /**
  245.      * The list of all widgets packed on the start
  246.      */
  247.     QList<KContainerLayoutItem> _startWidgets;
  248.     /**
  249.      * The list of all widgets packed at the end
  250.      */
  251.     QList<KContainerLayoutItem> _endWidgets;
  252.     /**
  253.      * width or height we have for each widget
  254.      */
  255.     int _sizeForEach;    
  256.     /**
  257.      * our sizeHint that we will return on our implementation of sizeHint()
  258.      */
  259.     QSize _sizeHint;
  260. };
  261.  
  262. #endif // __KCONTAINER_H__
  263.